package day14;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

public class IoTest {
	/**
	 * 读一个文件，输出到控制台
	 */
	@Test
	public void Iotest(){
		try {
			FileReader fileReader = new FileReader("G:/Test.txt");
			int i = -1;
			while((i = fileReader.read())!= -1){
				System.out.print((char)i);
			}
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	/**
	 * 字符流
	 * 读一个文件，并另存为
	 */
	@Test
	public void test1(){
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		
		char[] buff = new char[1024];
		try {
			fileReader = new FileReader("G:/Test.txt");
			fileWriter = new FileWriter("Test_back.txt");
			int length = 0;
			while((length = fileReader.read(buff)) != -1) {
				fileWriter.write(buff, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			if (fileReader != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 读一个文件并另存为
	 * 字节流
	 */
	@Test
	public void test2() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		
		try {
			fileInputStream = new FileInputStream("C:/Users/LLLLogan/Desktop/创作/me.jpg");
			fileOutputStream = new FileOutputStream("me_back.jpg");
			byte[] buffer = new byte[1024];
			int length = 0;
			while((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer,0,length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
